export default {
  /*
  * 过滤标签 空格等等
  * */
  filterHTMLTag (str) {
    let msg = str.replace(/<\/?[^>]*>/g, '') // 去除HTML Tag
    msg = msg.replace(/[|]*\n/, '') // 去除行尾空格
    msg = msg.replace(/&npsp;/ig, '') // 去掉npsp
    return msg
  },

  /*
  * window 窗口的宽高
  * */
  getWindowWH () {
    return {
      width: document.documentElement.clientWidth || document.body.clientWidth,
      height: document.documentElement.clientHeight || document.body.clientHeight
    }
  },

  /*
  * 指定标签元素的宽高
  * */
  getTargetWH (e) {
    return {
      width: e.offsetWidth,
      height: e.offsetHeight
    }
  },

  /*
  * 获取元素到视窗的距离
  * */
  getViewTBLR (el) {
    return {
      top: el.getBoundingClientRect().top,
      bottom: el.getBoundingClientRect().bottom,
      left: el.getBoundingClientRect().left,
      right: el.getBoundingClientRect().right
    }
  },

  /*
  * 判断是否进入视图
  * @param(el) domList dom元素数组
  * */
  isView (el) {
    const io = new IntersectionObserver(ioes => {
      ioes.forEach(e => {
        if (e.isIntersecting) {
          return true
        } else {
          return false
        }
      })
    })
    // Watch for intersection events on a specific target Element.
    // 对元素target添加监听，当target元素变化时，就会触发上述的回调
    io.observe(el)

    // Stop watching for intersection events on a specific target Element.
    // 移除一个监听，移除之后，target元素的可视区域变化，将不再触发前面的回调函数
    io.unobserve(el)

    // Stop observing threshold events on all target elements.
    // 停止所有的监听
    io.disconnect()
  },

  /*
  * 图片居中
  * 父元素relative，图片absolute
  * */
  imgCenter (e, num) {
    const width = this.getTargetWH(e).width
    const height = this.getTargetWH(e).height
    if (width === height) {
      e.style.height = 100 + '%'
      e.style.left = 0
    }
    if (width < num || height < num) {
      if (width > height) {
        let cha = (width - height) / 2
        e.style.height = 100 + '%'
        e.style.left = -cha + 'px'
        e.style.top = 0
      } else {
        let cha = (height - width) / 2
        e.style.width = 100 + '%'
        e.style.top = -cha + 'px'
        e.style.left = 0
      }
    } else {
      if (width > height) {
        let rate = num / height
        let chaResult = (width - height) * rate / 2
        e.style.height = 100 + '%'
        e.style.left = -chaResult + 'px'
        e.style.top = 0
      } else {
        let rate = num / width
        let chaResult = (height - width) * rate / 2
        e.style.width = 100 + '%'
        e.style.top = -chaResult + 'px'
        e.style.left = 0
      }
    }
  },

  // 返回数组中的某一字段字符串
  outputStr (arr) {
    const list = []
    arr.forEach(ele => {
      list.push(ele.id)
    })
    return list.join(',')
  },

  // 视觉滚动
  scrollTopTimer () {
    let scrollToptimer = setInterval(() => {
      var top = document.body.scrollTop || document.documentElement.scrollTop
      var speed = top / 4
      if (document.body.scrollTop !== 0) {
        document.body.scrollTop -= speed
      } else {
        document.documentElement.scrollTop -= speed
      }
      if (top === 0) {
        clearInterval(scrollToptimer)
      }
    }, 30)
  },

  // 兄弟节点
  siblings (elm) {
    var a = [] // 保存所有兄弟节点
    var p = elm.parentNode.children // 获取父级的所有子节点
    for (var i = 0; i < p.length; i++) { // 循环
      if (p[i].nodeType === 1 && p[i] !== elm) { // 如果该节点是元素节点与不是这个节点本身
        a.push(p[i]) // 添加到兄弟节点里
      }
    }
    return a
  }
}
